<?php

define('DATABASE','dealscombined');
require("connect.php");

// Select DB
$db=mysql_select_db(DATABASE,$connect);
if(!$db) {
die('Can\'t use DATABASE : '.mysql_error());
}

// English stopwords - used for all territories
$stopwords = array(
	'about', 'above', 'after', 'against', 'all', 'and', 'are', 'around', 'at',
	'because', 'before', 'below', 'but',
	'combine', 'combining', 'could',
	'depth', 'describe',
	'else', 'except',
	'feature', 'features', 'featuring', 'for', 'from', 'fully',
	'give', 'given',
	'had', 'has', 'have', 'having', 'height', "he'll", 'how', 'however',
	'in', 'includes', 'including', 'into', 'its',
	'length', 'less', 'like', 'look', 'looking', 'looks',
	'made', 'make', 'makes', 'might', 'more', 'most',
	'near', 'next', 'not',
	'of', 'off', 'one', 'onto', 'other', 'our', 'out', 'over',
	'per',
	'require', 'required', 'requires',
	'say', 'she', "she'll",
	'than', 'that', 'the', 'their', 'theirs', 'them', 'they', "they'll", "they're", 'this', 'these', 'those', 'through', 'to', 'too',
	'under', 'unlike', 'use', 'used', 'using',
	'was', 'what', 'when', 'where', 'who', 'width', 'will', 'with', 'within', 'without', 'would',
	'you', 'your', "you'd", "you'll", 'yours', 'yourself'
);

$result = mysql_query("SELECT * FROM products");

while($product = mysql_fetch_assoc($result)){
	$product['dc_category'] = getCategories($product['mapped_category']);
	$product['product_name'] = html_entity_decode($product['product_name']);
	$product['city_name'] = getCity($product['id']);
	$keywords = generateKeywords($product);
	
	if(strlen($product['product_name']) > strlen($product['description'])){
		
		$productName = $product['description'];
		$productDescription = $product['product_name'];
		mysql_query("UPDATE products SET product_name = '".mysql_real_escape_string($productName)."', description = '".mysql_real_escape_string($productDescription)." WHERE id =".$product['id']);
		
	}
	
	mysql_query("UPDATE products SET keywords = '".mysql_real_escape_string($keywords)."' WHERE id =".$product['id']);
}

function generateKeywords($product)
{
	$keywords = explode(' ', $product['product_name']);
	$keywords[] = $product['category_name'];
	$keywords[] = $product['location'];
	$keywords[] = $product['city_name'];
	$keywords[] = $product['dc_category'];
	$keywords[] = $product['promotional_text'];
	$keywords[] = $product['specifications'];
	$keywords[] = $product['warranty'];
	$keywords[] = $product['brand_name'];
	$keywords[] = $product['description'];
	
	$keywords = array_map('keywordTransform', $keywords);
	
	$keywords = array_filter($keywords, 'keywordFilter');
	$keywords = array_unique($keywords);

	return implode(', ', $keywords);
}

function keywordFilter($element)
{		
	return !is_null($element) && $element != '' && mb_strlen($element) > 1 && array_search(strtolower($element), $GLOBALS['stopwords']) === false;
}

function keywordTransform($element)
{
	return strtolower(trim(str_replace(array(',', '.', ';', ':', '?', '!', '"', "'", ")", "(", ">", "<"), '', $element)));
}

function getCategories($categoryId)
{
	$query = "SELECT c.category, s.category AS subcategory FROM subcategories s INNER JOIN categories c ON s.category_id = c.id WHERE s.id = $categoryId";
	
	$result = mysql_query($query);
	
	$category = NULL;
	
	if($result){
		while($row = mysql_fetch_assoc($result)){
			
			$category = $row['category'] .' > ' . $row['subcategory'];
			
		}
	}
	
	return $category;
	
}

function getCity($productId)
{
	$query = "SELECT c.name FROM products p INNER JOIN cities c ON p.city_id = c.id WHERE p.id = $productId";
	
	$result = mysql_query($query);
	
	$city = NULL;
	
	while($row = mysql_fetch_assoc($result)){
		
		$city = $row['name'];
		
	}
	
	return $city;
	
}


?>